﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;

namespace Alarm
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        List<MyAlarm> times = new List<MyAlarm>();

        private System.Windows.Forms.NotifyIcon notifyIcon;
        private System.Windows.Forms.ContextMenu contextMenu;
        private bool toClose;

        public MainWindow()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
            this.Closing += new System.ComponentModel.CancelEventHandler(MainWindow_Closing);
            this.Closed += new EventHandler(MainWindow_Closed);
            this.StateChanged += new EventHandler(MainWindow_StateChanged);
            this.Visibility = System.Windows.Visibility.Hidden;

            this.contextMenu = new System.Windows.Forms.ContextMenu();

            System.Windows.Forms.MenuItem menuItemExit = new System.Windows.Forms.MenuItem("Exit");
            menuItemExit.Click += new EventHandler(menuItemExit_Click);
            this.contextMenu.MenuItems.Add(menuItemExit);

            this.notifyIcon = new System.Windows.Forms.NotifyIcon();
            this.notifyIcon.Text = "Alarm";
            this.notifyIcon.ContextMenu = this.contextMenu;

            this.notifyIcon.Click += new EventHandler(notifyIcon_Click);
            this.notifyIcon.DoubleClick += new EventHandler(notifyIcon_DoubleClick);

            this.notifyIcon.Icon = new System.Drawing.Icon(@"ProgressWarn.ico");
            this.notifyIcon.Visible = true;

            this.toClose = false;

            this.btnAdd.Click += new RoutedEventHandler(btnAdd_Click);

            Thread thr = new Thread(new ThreadStart(this.Count));
            thr.Start();
        }

        void MainWindow_StateChanged(object sender, EventArgs e)
        {
            switch (this.WindowState)
            {
                case WindowState.Maximized:
                    break;
                case WindowState.Minimized:
                    {
                        this.Visibility = System.Windows.Visibility.Hidden;
                        break;
                    }
                case WindowState.Normal:
                    break;
                default:
                    break;
            }
        }

        object myLock = new object();
        void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            lock (this.myLock)
            {
                MyAlarm myAlarm = new MyAlarm();

                myAlarm.Time = this.txtTime.Text;
                myAlarm.Message = this.txtMsg.Text;

                this.txtTime.Text = "";
                this.txtMsg.Text = "";

                this.Visibility = System.Windows.Visibility.Hidden;

                this.times.Add(myAlarm);
                this.lstTimes.Items.Add(myAlarm.Time + " - " + myAlarm.Message);
            }
        }

        public void Count()
        {
            while (!this.toClose)
            {
                for (int i = 0; i < this.times.Count; i++)
                {
                    if (this.times[i].Time == DateTime.Now.ToShortTimeString() &&
                        (DateTime.Now - this.times[i].lstAlarmed).TotalDays >= 1)
                    {
                        this.alarm(this.times[i].Message);
                        this.times[i].update();
                    }
                }

                Thread.Sleep(500);
            }
        }


        public delegate void _alarm(string msg);
        public void alarm(string msg)
        {
            if (!this.Dispatcher.CheckAccess())
            {
                this.Dispatcher.Invoke(new _alarm(this.alarm), msg);
                return;
            }

            MessageBox.Show(msg);
        }

        void menuItemExit_Click(object sender, EventArgs e)
        {
            this.toClose = true;
            this.Close();
        }

        void MainWindow_Closed(object sender, EventArgs e)
        {
            this.notifyIcon.Dispose();
        }

        void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = !this.toClose;
            this.Visibility = System.Windows.Visibility.Hidden;
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            //this.ShowInTaskbar = false;

        }

        public void notifyIcon_DoubleClick(object sender, EventArgs e)
        {
            this.Visibility = System.Windows.Visibility.Visible;
        }

        public void notifyIcon_Click(object sender, EventArgs e)
        {

        }
    }

    public class MyAlarm
    {
        public DateTime lstAlarmed { get; set; }
        public string Message { get; set; }
        public string Time { get; set; }

        internal void update()
        {
            this.lstAlarmed = DateTime.Now;
        }
    }
}
